package com.example.malltest0.controller;


import com.example.malltest0.common.api.CommonResult;
import com.example.malltest0.dto.UmsAdminLoginParam;
import com.example.malltest0.mbg.model.UmsAdmin;
import com.example.malltest0.mbg.model.UmsPerssion;
import com.example.malltest0.service.AdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
@Api(tags = "UmsAdminController", description = "后台用户管理")
public class UmsAdminController {

    @Autowired
    private AdminService adminService;

    @Value("${jwt.tokenHeader}")
    private String tokenHeader;
    @Value("${jwt.tokenHead}")
    private String tokenHead;

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "用户注册")
    public CommonResult<UmsAdmin> register(@RequestBody UmsAdmin umsAdminParam) {
        UmsAdmin umsAdmin = adminService.register(umsAdminParam);
        if (umsAdmin == null) {
            CommonResult.failed();
        }
        return CommonResult.success(umsAdmin);
    }

    @ApiOperation(value = "登录以后返回token")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult login(@RequestBody UmsAdminLoginParam umsAdminLoginParam, BindingResult result) {
        String token = adminService.login(umsAdminLoginParam.getUsername(), umsAdminLoginParam.getPassword());
        if (token == null) {
            return CommonResult.validateFailed("用户名或密码错误");
        }
        Map<String, String> tokenMap = new HashMap<>();
        tokenMap.put("token", token);
        tokenMap.put("tokenHead", tokenHead);
        return CommonResult.success(tokenMap);
    }

    @ApiOperation("获取用户所有权限（包括+-权限）")
    @RequestMapping(value = "/permission/{adminId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<UmsPerssion>> getPermissionList(@PathVariable Long adminId) {
        List<UmsPerssion> permissionList = adminService.getPermissionList(adminId);
        return CommonResult.success(permissionList);
    }

    @ApiOperation("关注")
    @RequestMapping(value = "/star", method = RequestMethod.POST)
    @ResponseBody
    public  CommonResult follow(@RequestParam Long fid, Principal principal){
        String name = principal.getName();
        int count=adminService.follow(name,fid);
        return CommonResult.success("成功");
    }

    @ApiOperation("取关")
    @RequestMapping(value = "/unstar", method = RequestMethod.DELETE)
    @ResponseBody
    public  CommonResult unfollow(@RequestParam Long fid, Principal principal){
        String name = principal.getName();
        int count=adminService.unfollow(name,fid);
        return CommonResult.success("成功");
    }
}
